package com.itheima.health.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.health.common.MessageConst;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.Menu;
import com.itheima.health.pojo.Permission;
import com.itheima.health.pojo.Role;
import com.itheima.health.service.UserRoleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author ：lizimo
 * @date ：Created in 2020/1/2
 * @description ：角色控制器
 * @version: 1.0
 */
@Slf4j
@RequestMapping("/role")
@RestController
public class UserRoleController {

    @Reference
    private UserRoleService userRoleService;

    /**
     * 添加
     *
     * @param role
     * @param permissionIds
     * @return
     */
    @RequestMapping("/add")
    public Result add(@RequestBody Role role, Integer[] permissionIds, Integer[] menuIds) {
        log.info("[角色-添加]data:{},permissionId:{}", role, permissionIds);

        //RPC请求添加
        userRoleService.add(role, permissionIds, menuIds);
        return new Result(true, MessageConst.ADD_ROLE_SUCCESS);
    }

    @RequestMapping("/findAllMenu")
    public Result findAllMenu() {
        log.info("[角色-菜单查询]");
        //rpc调用业务查询
        List<Menu> menuList = userRoleService.findAllMenu();

        return new Result(true, MessageConst.QUERY_MENU_SUCCESS, menuList);
    }


    /**
     * 分页查询
     *
     * @param queryPageBean
     * @return
     */
    @RequestMapping("/findPage")
    public Result findPage(QueryPageBean queryPageBean) {
        log.info("[角色-分页查询]data:{}", queryPageBean);
        //rpc查询结果
        PageResult pageResult = userRoleService.pageQuery(queryPageBean);

        return new Result(true, MessageConst.QUERY_ROLE_SUCCESS, pageResult);
    }

    /**
     * 根据Id查询
     *
     * @param id
     * @return
     */
    @RequestMapping("findById")
    public Result findById(Integer id) {
        log.info("[角色]data:{}", id);
        //rpc调用业务处理
        Role role = userRoleService.findById(id);

        return new Result(true, MessageConst.QUERY_ROLE_SUCCESS, role);
    }

    @RequestMapping("/findMenuByRole")
    public Result findMenuByRole(Integer id) {
        log.info("[角色]-根据角色查询菜单data:{}",id);


       List<Menu> menuList2 =  userRoleService.findMenuByRole(id);

       return new Result(true,MessageConst.QUERY_MENU_SUCCESS,menuList2);
    }

    /**
     * 编辑角色列表
     * @param role
     * @param permissionIds
     * @param menuIds
     * @return
     */
    @RequestMapping("/edit")
    public Result edit(@RequestBody Role role,Integer[] permissionIds,Integer[] menuIds) {
        log.info("[角色]-编辑data:{},permissionIds:{},menuIds:{}", role, permissionIds, menuIds);

        userRoleService.edit(role,permissionIds,menuIds);
        return new Result(true,MessageConst.EDIT_ROLE_SUCCESS);

    }

    @RequestMapping("/findPermissionByRole")
    public Result findPermissionByRole(Integer id){
        log.info("[角色]-根据角色ID查询权限data:{}",id);
        List<Permission> permissionList2 = userRoleService.findPermissionByRole(id);
        return new Result(true,MessageConst.QUERY_PERMISSION_SUCCESS,permissionList2);

    }
    @RequestMapping("/findMenuByRoleId")
    public Result findMenuByRoleId(Integer id){
        log.info("[角色-根据角色ID查询菜单列表]");
        List<Menu> menuList2 = userRoleService.findMenuByRoleId(id);
        return new Result(true,MessageConst.QUERY_MENU_SUCCESS,menuList2);
    }

    @RequestMapping("/findPermissionIdByRoleId")
    public  Result findPermissionIdByRoleId(Integer id){
        log.info("[角色-根据角色ID查询权限ID]data:{}",id);
        List<Permission> permissionList = userRoleService.findPermissionIdByRoleId(id);

        return new Result(true,MessageConst.QUERY_PERMISSION_SUCCESS,permissionList);
    }


    @RequestMapping("delete")
    public Result delete(Integer id) {
        log.info("[权限-删除]data:{}", id);
        //rpc调用
        userRoleService.delete(id);
        return new Result(true, MessageConst.DELETE_ROLE_SUCCESS);

    }

}
